﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using MyLocalChurch.Business;
using MyLocalChurch.Constants;
using MyLocalChurch.Models;
using MyLocalChurch.Models.Enumerations;
using MyLocalChurch.Models.KidsChurch;
using RKConsulting.Helpers;
using RKConsulting.Web.Helpers;

namespace MyLocalChurch.KidsChurch
{
    public partial class KidsList : System.Web.UI.Page
    {
        private readonly MyContext _context = MyContextSingleton.Instance;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                return;

            var guardianId = QueryHelper.GetNullableIntQueryParam(QueryParameters.GuardianId);
            var guardian = GuardianManager.GetGuardianAndUpdateCache(_context, guardianId);

            List<Kid> kids;
            if (guardian != null)
            {
                panelRegisterKid.Visible = true;
                lblHeader.Text = string.Format("Kids of {0}", guardian);
                kids = guardian.Kids;
            }
            else
            {
                panelRegisterKid.Visible = false;
                lblHeader.Text = "Kids";
                kids = _context.Kids.ToList();
            }

            rptKids.DataSource = kids;
            rptKids.DataBind();
        }

        protected void rptKids_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;

            var kid = (Kid) e.Item.DataItem;
            var lblKidCategory = (Label)e.Item.FindControl("lblKidCategory");
            var linkBarcode = (HtmlAnchor)e.Item.FindControl("linkBarcode");
            var lblBarcode = (Label)e.Item.FindControl("lblBarcode");
            var linkCheckInOrOut = (HtmlAnchor)e.Item.FindControl("linkCheckInOrOut");
            var lblCheckIn = (Label)e.Item.FindControl("lblCheckIn");

            var kidCategory = KidManager.GetKidCategory(kid);
            if (kidCategory == KidCategory.NotAKid)
            {
                linkCheckInOrOut.Visible = false;
                lblCheckIn.Visible = true;
            }
            else
            {
                var checkIn = CheckInOutManager.GetUnCheckedOut(kid.CheckInOuts);

                if (checkIn == null)
                {
                    linkCheckInOrOut.Visible = true;
                    lblCheckIn.Visible = false;
                    
                    linkCheckInOrOut.HRef = string.Concat("CheckIn.aspx?", QueryParameters.KidId, "=", kid.Id);
                }
                else if (kidCategory == KidCategory.PreTeen)
                {
                    linkCheckInOrOut.Visible = true;
                    lblCheckIn.Visible = false;

                    linkCheckInOrOut.InnerText = "Check Out";
                    linkCheckInOrOut.HRef = string.Concat("CheckOut.aspx?", QueryParameters.KidId, "=", kid.Id);
                }
                else
                {
                    linkCheckInOrOut.Visible = false;
                    lblCheckIn.Visible = true;
                }
            }

            lblKidCategory.Text = EnumHelper.ToSpacesString(kidCategory);

            var barcode = kid.GetValidBarcode();
            if (barcode == null)
            {
                linkBarcode.Visible = false;
                lblBarcode.Visible = true;
            }
            else
            {
                linkBarcode.Visible = true;
                lblBarcode.Visible = false;

                linkBarcode.HRef = string.Concat("/Admin/ViewBarcode.aspx?", QueryParameters.Barcode, "=",
                                                 barcode.Code);
            }
        }
    }
}